<?php
/* @var $this SiteController */
$this->pageTitle=Yii::app()->name;

?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/assets/js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/assets/js/jquery-ui-1.10.3.min.js"></script>
        <!--ckeditor-->
<!--        <script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/ckeditor/ckeditor.js"></script>
        <script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/ckeditor/config.js"></script>-->
        <!--elFinder CSS (REQUIRED)--> 
        <link rel="stylesheet" type="text/css" media="screen" href="<?php echo Yii::app()->baseUrl ?>/css/elfinder.min.css">
        <link rel="stylesheet" type="text/css" media="screen" href="<?php echo Yii::app()->baseUrl ?>/css/theme.css">
        
        <!--elFinder JS (REQUIRED)--> 
        <script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/js/elfinder.min.js"></script>
        
        <!--elFinder translation (OPTIONAL)--> 
       <!--<script type="text/javascript" src="<?php echo Yii::app()->baseUrl ?>/js/i18n/elfinder.ru.js"></script>-->
        
        <!--elFinder initialization (REQUIRED)--> 
        <script type="text/javascript" charset="utf-8">
            $().ready(function() {
            });
        </script>
        <!-- ace styles -->
        <link href="<?php echo Yii::app()->baseUrl."assets/css/ace.min.css" ?>" rel="stylesheet" />
        <link href="<?php echo Yii::app()->baseUrl."assets/css/ace-rtl.min.css" ?>" rel="stylesheet" />
        <link href="<?php echo Yii::app()->baseUrl."assets/css/ace-skins.min.css" ?>" rel="stylesheet" />      
        <script type="text/javascript">
            window.jQuery || document.write("<script src='<?php echo Yii::app()->baseUrl."/assets/js/jquery-2.0.3.min.js"?>'>"+"<"+"/script>");
        </script>
        <script type="text/javascript">
            if("ontouchend" in document) document.write("<script src='<?php echo Yii::app()->baseUrl."/assets/js/jquery.mobile.custom.min.js"?>'>"+"<"+"/script>");
        </script>
        <script src="<?php echo Yii::app()->baseUrl."/assets/js/bootstrap.min.js" ?>"></script>
        <script src="<?php echo Yii::app()->baseUrl."/assets/js/typeahead-bs2.min.js"?>"></script>
        <!-- ace scripts -->
        <script src="<?php echo Yii::app()->baseUrl."/assets/js/ace-elements.min.js"?>"></script>
        <script src="<?php echo Yii::app()->baseUrl."/assets/js/ace.min.js"?>"></script>
        <script src="<?php echo Yii::app()->baseUrl."/assets/dropzone-3.10.2/downloads/dropzone.js"?>"></script>
        <script src="<?php echo Yii::app()->baseUrl."/assets/dropzone-3.10.2/downloads/dropzone.min.js"?>"></script>
        <link rel="sytesheet" type="text/css" media="screen" href="<?php echo Yii::app()->baseUrl."/assets/dropzone-3.10.2/downloads/css/dropzone.css"?>">
        
        <title></title>
    </head>
    <body>
        <?php
         try{
             //load data and load category for select
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT *  FROM os_category WHERE os_category.sale_id =".Yii::app()->session["saleId"];
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_category = array();

                while(($row=$dataReader->read())!==false) {
                    $rows_category[]=$row;
                }
                
                $query_2 = "select * from os_item where os_item.sale_id =".Yii::app()->session["saleId"];
                $command_2 =$connection->createCommand($query_2);
                $dataReader_2=$command_2->query();
                $rows_items = array();
                while(($row=$dataReader_2->read())!==false) {
                    $rows_items[]=$row;
                }
                
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
             }        
        ?>
        <div id="main-container" class="main-container">
            <div id="breadcrumbs" class="breadcrumbs">
                <script type="text/javascript">
                    try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){};
                </script>
                <ul class="breadcrumb">
                    <li>
                        <i class="icon-home home-icon"></i>
                        <a href="../site/index"><strong>Trang Chủ</strong></a>
                    </li>
                    <li>
                        <a href="../site/item"><strong>Sản Phẩm</strong></a>
                    </li>                                                                                                                                       
                    <li>
                        <strong> Chỉnh Sửa Sản Phẩm </strong>
                    </li>
                </ul><!-- .breadcrumb -->
                
                <div id="nav-search" class="nav-search">
                    <form class="form-search">
                        <span class="input-icon">
                            <input type="text" autocomplete="off" id="nav-search-input" class="nav-search-input" placeholder="Search ...">
                            <i class="icon-search nav-search-icon"></i>
                        </span>
                    </form>
                </div><!-- #nav-search -->
            </div>    
            
            <div class="page-content">
                <div class="page-header">
                    <h1><strong> Chỉnh Sửa Sản Phẩm</strong></h1>
                </div><!-- /.page-header -->
                <div class="row">
                    <div class="col-xs-12">
            <?php $form=$this->beginWidget('CActiveForm', array(
                'htmlOptions' => array(
                    'enctype' => 'multipart/form-data',
                ),              
            ));?>
                        <!--<form class="form-horizontal" method="POST" enctype="multipart/form-data">-->
                        <fieldset>
                            <div class="form-group">
                        <?php // echo $form->error($model,'name');?>
                                <label class="col-sm-2 control-label no-padding-right">
                                    Tên Sản Phẩm
                                </label>
                                <div class="col-sm-10">
                                    <input class="col-xs-10 col-sm-5" type="text" name="ItemForm[name]" id="ItemForm_name" value="<?php echo $rows_items[$_GET['id']-1]["NameItem"]?>"></input>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            
                            <div class="form-group">
                        <?php // echo $form->error($model,'name');?>
                                <label class="col-sm-2 control-label no-padding-right">
                                    Nhóm Sản Phẩm
                                </label>
                                <div class="col-sm-10">
                                    <select class="col-xs-10 col-sm-5" name="ItemForm[IdCategory]" id="ItemForm_IdCategory">
                                        <option value="">-- Chọn một Nhóm Sản Phẩm --</option>
                        <?php
                        foreach($rows_category as $key => $value):
                            if ($value['Id'] == $rows_items[$_GET['id']-1]['IdCategory']) {
                        echo "<option selected value={$value['Id']} >{$value['NameCategory']}</option>"; //close your tags!!                             
                         } else {
                         echo "<option value={$value['Id']} >{$value['NameCategory']}</option>";}
                        endforeach;
                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            
                            <div class="form-group">
                        <?php // echo $form->error($model,'name');?>
                                <label class="col-sm-2 control-label no-padding-right">
                                    Giá Sản Phẩm
                                </label>
                                <div class="col-sm-10">
                                    <input class="col-xs-10 col-sm-5" type="number" name="ItemForm[price]" id="ItemForm_price" value="<?php echo $rows_items[$_GET['id']-1]["Price"]?>"></input>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right">
                                    Thông Tin Chi Tiết
                                </label>
                                <div class="col-sm-10">
                                    <textarea class="col-xs-10 col-sm-5" cols="20" rows="5" name="ItemForm[description]" id="ItemForm_description"><?php echo $rows_items[$_GET['id']-1]["Description"]?></textarea>
                                    <!--<input class="col-xs-10 col-sm-5" type="text" name="NewsForm[content]" id="NewsForm_content"></input>-->
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            
                            <div class="form-group"> 
                                <label class="col-sm-2 control-label no-padding-right">
                                    Ảnh chính
                                </label>
                                <div class="col-sm-10" >
                            <?php  if($rows_items[$_GET['id']-1]["LinkImage"] != NULL){ ?>
                            <?php
//                            echo CHtml::image(Yii::app()->request->baseUrl.'/files/'.$rows_items[$_GET['id']-1]["LinkImage"],"Image Invalid",array("width"=>200)); 
//                            }
                            ?>                        
                            <?php // echo CHtml::activeFileField($model, 'image'); ?>
                                    <img src="../../files/<?php echo $rows_items[$_GET['id']-1]['LinkImage'];?>" onclick="imageClick();" id="LinkImage" name="LinkImage" style="max-height: 600px; max-width: 800px;"/>
                             <?php } else {?>
                                    <img src="../../images/photo.png" onclick="imageClick();" id="LinkImage" name="LinkImage" style="max-height: 600px; max-width: 800px;"/>
                             <?php }?>
                                    <input type="hidden" id="ItemForm_image" name="ItemForm[image]" type="text"/>
                                    <p class="help-block">                                
                                        Image must be more than 100x100px and smaller than 3MB
                                    </p>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right">
                                    Ảnh phụ
                                </label>
                                <div class="col-sm-10">
                                    <div class="col-xs-10 col-sm-5">               
                                        <?php //  if($rows_items[$_GET['id']-1]["list_image"] != NULL){ ?>
                                        <?php //echo CHtml::image(Yii::app()->request->baseUrl.'/files/'.$rows_items[$_GET['id']-1]["LinkImage"],"Image Invalid",array("width"=>200));} ?>          
                                        <?php 
                                        $list_image = json_decode($rows_items[$_GET['id']-1]["list_image"]);
//                                        echo var_dump($list_image);
                                        if (!empty($list_image)) {
                                        foreach ($list_image as $key =>$value) {
                                            if(!empty($value)) {
//                                        echo var_dump($list_image[$key]);
                                        ?>                                        
                                        <img src="../../files/<?php echo $list_image[$key];?>" onclick="imageSub(<?php echo $key?>);" id="list_image_<?php echo $key?>" name="ItemForm[sub_image]" style="max-height: 200px; max-width: 200px;"/>
                                        <input type="hidden" id="ItemForm_listimage_<?php echo $key?>" name="ItemForm[sub_image][]" type="text"/>
                                        <?php }                                      
                                            }} else {?>
                                        <img src="../../images/photo.png" onclick="imageSub('tmp');" id="list_image_tmp" name="ItemForm[sub_image]" style="max-height: 200px; max-width: 200px;"/>
                                        <input type="hidden" id="ItemForm_listimage_tmp" name="ItemForm[sub_image][]" type="text"/>
                                        <?php }?>
                                        <!--test-->
                                        <?php // $id = $key;
//                                        echo "<img  src='../../images/add_icon.jpg' onclick='myFunction(".($id+=1).")'/>"?>
<!--                                        <img id ="placehere_<?php // echo $id;?>"/>-->
                                        <!--<input type="hidden" id="ItemForm_listimage_<?php // echo $id?>" name="ItemForm[sub_image][]" type="text"/>-->
                                        <div class="clearfix"></div>
                                        <img  src='../../images/add_icon.jpg' onclick='myFunction()' style="max-height: 25px; max-width: 25px"/>
                                        <div id ="placehere"></div>
                                        <p class="help-block">                                
                                        Image must be more than 100x100px and smaller than 2MB
                                    </p>
                                        
                                        
                                        <!--                                        <h4 class="pink">									
                                                                                    <i class="icon-plus green"></i>
                                                                                    <a href="#modal-form" role="button" class="blue" data-toggle="modal"> Thêm ảnh </a>                                       
                                                                                </h4>-->
                                        
                                            <!--                                        <input multiple="true" type="file" id="id-input-file" name="ItemForm[]"/>       
                                        -->
                                    </div>
                                    <div class="col-xs-10 col-sm-5" style="color: red">
                                        <?php echo $form->error($model,'sub_image')?>
                                    </div>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            <div class="form-group">
                        <?php // echo $form->error($model,'name');?>
                                <label class="col-sm-2 control-label no-padding-right">
                                    Giảm Giá
                                </label>
                                <div class="col-sm-10">
                                    <input class="col-xs-10 col-sm-5" type="number" name="ItemForm[discount]" id="ItemForm_discount" value="<?php echo $rows_items[$_GET['id']-1]["Discount"]?>"></input>
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div class="space-6"></div>
                            
                            <div class="form-group">
                        <?php // echo $form->error($model,'name');?>
                                <label class="col-sm-2 control-label no-padding-right">
                                    Đơn Vị Tính
                                </label>
                                <div class="col-sm-10">
                                    <input class="col-xs-10 col-sm-5" type="text" name="ItemForm[unit]" id="ItemForm_unit" value="<?php echo $rows_items[$_GET['id']-1]["Unit"]?>"></input>
                                </div>
                            </div>
                            <div class=" clearfix"></div>
                            <div class="clearfix form-actions">
                                <div class="col-md-offset-2 col-md-10">
                                    <button class="btn btn-sm btn-primary" type="submit" name="submit" value="submit">
                                        <i class="icon-save align-top bigger-125"></i>
                                        Save
                                    </button>
                                    <a class="btn btn-sm" type="button" href="../site/item">                                
                                        Back
                                        <i class="icon-backward align-top bigger-125"></i>
                                    </a>
                                </div>
                            </div>
                        </fieldset>
                        <!--</form>-->
            <?php $this->endWidget(); ?>
                    </div>
                </div>
                
            </div>
        </div>
        <script type="text/javascript" charset="utf-8">         
            //            function myFunction(id)
            //{
            ////    id += 1;
            //var elem = document.createElement("img");
            //elem.setAttribute("src", "../../images/photo.png");
            //elem.setAttribute("height", "300");
            //elem.setAttribute("width", "300");
            //elem.setAttribute("alt", "Image");
            ////elem.setAttribute("id","list_image_"+id);
            //elem.setAttribute("onclick","imageSub("+id+")");
            //document.getElementById("placehere_"+id).appendChild(elem);
            //}

//            jQuery(function($) {
//                $('#id-input-file').ace_file_input({
//                    style:'well',
//                    btn_choose:'Drop files here or click to choose',
//                    btn_change:null,
//                    no_icon:'icon-cloud-upload',
//                    droppable:true,
//                    thumbnail:'small'//large | fit
//                    //,icon_remove:null//set null, to hide remove/reset button
//                    /**,before_change:function(files, dropped) {
//                                                //Check an example below
//                                                //or examples/file-upload.html
//                                                return true;
//                                        }*/
//                    /**,before_remove : function() {
//                                                return true;
//                                        }*/
//                    ,
//                    preview_error : function(filename, error_code) {                
//                        //name of the file that failed
//                        //error_code values
//                        //1 = 'FILE_LOAD_FAILED',
//                        //2 = 'IMAGE_LOAD_FAILED',
//                        //3 = 'THUMBNAIL_FAILED'
//                        //alert(error_code);
//                    }
//			
//                }).on('change', function(){
//                    //console.log($(this).data('ace_input_files'));
//                    //console.log($(this).data('ace_input_method'));
//                });
//            });
            function myFunction() {
                var elem = document.createElement("input");
                elem.setAttribute("multiple","true");
                elem.setAttribute("id","id-input-file");
                elem.setAttribute("name", "ItemForm[]");
                elem.setAttribute("type","file");    
                document.getElementById("placehere").appendChild(elem);
            };
            function imageClick(){
                $( "#dialog-message" ).dialog({
                    title:"",
                    height:600,
                    width:1100,
                    modal: true
                });					
                var elf = $('#elfinder').elfinder({
                    url : '<?php echo Yii::app()->baseUrl ?>/php/connector.php' // connector URL (REQUIRED)
                    ,lang: 'en',
                    width:'100%',
                    commandsOptions: {
                        getfile: {
                            oncomplete: 'destroy' 
                        }
                    },
                    getFileCallback: function(file) {
                        $('#LinkImage').attr('src',file);
                        //                       $('#CategoryForm_image').attr('value',file);
                        //                            $('#CategoryForm_image').attr('src',file);
                        $('#ItemForm_image').attr('value',file);
                        $( "#dialog-message" ).dialog( "close" );
                    }
                }).elfinder('instance'); 
                
            };
            function imageSub(key){
                $( "#dialog-message" ).dialog({
                    title:"",
                    height:600,
                    width:1100,
                    modal: true
                });					
                var elf = $('#elfinder').elfinder({
                    url : '<?php echo Yii::app()->baseUrl ?>/php/connector.php' // connector URL (REQUIRED)
                    ,lang: 'en',
                    width:'100%',
                    commandsOptions: {
                        getfile: {
                            oncomplete: 'destroy' 
                        }
                    },
                           
                    getFileCallback: function(file) {
                        $("#list_image_"+key).attr('src',file);
                        //                       $('#CategoryForm_image').attr('value',file);
                        //                            $('#CategoryForm_image').attr('src',file);
                        //                        $("#placehere_"+key).attr('src',file);
                        $("#ItemForm_listimage_"+key).attr('value',file);
                        $( "#dialog-message" ).dialog( "close" );
                    }
                }).elfinder('instance'); 
                
            };
//            function newImage() {
//                window.close();
                //            var idparent = $()
                //                $.ajax({ type:'POST',
                //                    url: 'site/newImage',
                //                    success: function(data) {
                //                        alert(data);
                ////                        $("#modal-form").dialog("close");
                //                    },
                //                });
//            };
            $('#modal-form input[type=file]').ace_file_input({
                style:'well',
                btn_choose:'Drop files here or click to choose',
                btn_change:null,
                no_icon:'icon-cloud-upload',
                droppable:true,
                thumbnail:'large'
            })				
            //chosen plugin inside a modal will have a zero width because the select element is originally hidden
            //and its width cannot be determined.
            //so we set the width after modal is show
            $('#modal-form').on('shown.bs.modal', function () {
                $(this).find('.chosen-container').each(function(){
                    $(this).find('a:first-child').css('width' , '210px');
                    $(this).find('.chosen-drop').css('width' , '210px');
                    $(this).find('.chosen-search input').css('width' , '200px');
                });
            })
        </script>
        <div id="dialog-message" >
            <div id="elfinder"></div>
        </div>
        <div id="modal-form" class="modal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="blue bigger">Chọn ảnh</h4>
                        <input multiple="true" type="file" id="id-input-file" name="ItemForm[sub_image][]"/>
                    </div>                
                    <div class="modal-footer">
                        <button class="btn btn-sm" data-dismiss="modal">
                            <i class="icon-remove"></i>
                            Cancel
                        </button>                    
                        <button class="btn btn-sm btn-primary">
                            <i class="icon-ok"></i>
                            Save
                        </button>
                    </div>
                </div>
            </div>
        </div><!-- PAGE CONTENT ENDS -->
    </body>
</html>
